package com.vimaltuts.mycontacts;

import java.util.ArrayList;

/*import pill.Reminder.AddSchedule_act;
import pill.Reminder.Display_pilldetails_act;
import pill.Reminder.F2Activity;*/

import com.vimaltuts.mycontacts.R;


import android.os.AsyncTask;
import android.os.Bundle;
import android.app.ListActivity;
import android.content.Intent;
import android.database.Cursor;
import android.view.ContextMenu;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ContextMenu.ContextMenuInfo;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;

public class MyContactsActivity extends ListActivity {
	
	ArrayList<String> names;
	private ListView lview;
	
	SQLiteConnector sqlCon;
	private CustomContactsAdapter custAdapter;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);		
		String[] from = new String[] { Contacts.NAME, Contacts.PHONE,Contacts.MAIL };
		int[] to = new int[] { R.id.name, R.id.phone, R.id.email};
		/*int[] to = new int[] { R.id.name, R.id.phone, R.id.email };*/
		custAdapter = new CustomContactsAdapter(this,R.layout.contact_list_item, null, from, to);
		this.setListAdapter(custAdapter);	
		this.registerForContextMenu(getListView());
		

		/*lview = (ListView) findViewById(R.id.name);

		registerForContextMenu(lview);*/
		
		//registerForContextMenu(custAdapter);
				
		
	}

	@Override
	protected void onResume() {
		super.onResume();
		new GetContacts().execute((Object[]) null);
	}

	@SuppressWarnings("deprecation")
	@Override
	protected void onStop() {
		Cursor cursor = custAdapter.getCursor();

		if (cursor != null)
			cursor.deactivate();

		custAdapter.changeCursor(null);
		super.onStop();

	}

	private class GetContacts extends AsyncTask<Object, Object, Cursor> {
		SQLiteConnector dbConnector = new SQLiteConnector(MyContactsActivity.this);

		@Override
		protected Cursor doInBackground(Object... params) {
			return dbConnector.getAllContacts();
		}

		@Override
		protected void onPostExecute(Cursor result) {
			custAdapter.changeCursor(result); 
		}
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.activity_my_contacts, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		Intent addContact = new Intent(MyContactsActivity.this,AddEditContacts.class);
		startActivity(addContact);
		return super.onOptionsItemSelected(item);
	}
	
	public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo)
	{
		if (v.getId() == R.id.name)
		{
			AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) menuInfo;
			menu.setHeaderTitle(names.get(info.position).toString());
			String[] menuItems; //0-patient & 1-admin
			 menuItems = getResources().getStringArray(R.array.menu_pat);
			for (int i = 0; i < menuItems.length; i++)
			{
				menu.add(Menu.NONE, i, i, menuItems[i]);
			}
		}
	}
	
	/*@Override
	public boolean onContextItemSelected(MenuItem item)
	{

		AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
		int menuItemIndex = item.getItemId();
		String[] menuItems = getResources().getStringArray(R.array.menu_pat);
		String menuItemName = menuItems[menuItemIndex];
		String listItemName = names.get(info.position);

		switch (menuItemIndex)
		{
			case 0:
				{
					Display_pilldetails_act.startActivity(F2Activity.this, names.get(info.position),0);
					break;
				}
			case 1:
				
				{
					createDialog( idList.get(info.position));
					break;
				}
			case 2:	
				{
					AddSchedule_act.startActivity(F2Activity.this, idList.get(info.position));
					break;
				}
			default:
				Toast.makeText(this, "Error: Cannot execute option " + listItemName, Toast.LENGTH_LONG).show();
				break;
		}


		return true;
	}*/

	
}
